﻿using Api.WDS.Methods;
using Method.DAL;
using Models.operResult;
using Models.VModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Operate
{
    public class ArticleOp
    {
        /// <summary>
        /// 发表文章
        /// </summary>
        /// <returns></returns>
        public OperResult PublishArticle(V_Publish v_Publish)
        {
            var uid = new GetUidByToken().GetUid(v_Publish.Token);
            if (uid is null)
                return OperResult.Failed("请重新登录");

            if (v_Publish.Title is { Length: 0 })
                return OperResult.Failed("文章内容不正确");
            if (v_Publish.Context.Length < 10)
                return OperResult.Failed("文章内容不正确");

            return new ArticleMet().CreatetArticle(Convert.ToInt32(uid), v_Publish);
        }

        /// <summary>
        /// 封建用户文章
        /// </summary>
        /// <param name="token"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public OperResult DelArticle(string token, string articleId)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult.Failed("请重新登录");
            if(!int.TryParse(articleId,out int i))
                return OperResult.Failed("查找文章失败");

            if (new ArticleMet().DelArticle(Convert.ToInt32(uid), Convert.ToInt32(articleId)))
                return OperResult.Succeed();
            return OperResult.Failed("删除失败"); ;
        }
    }
}
